package org.xg.study.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author CMG
 * @version 1.0
 * @description
 * @cLassName LoginServlet
 * @date 2021/9/24 16:27
 **/
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置页面输出格式
        resp.setContentType("text/html;charset=UTF-8");
        req.setCharacterEncoding("utf-8");
        PrintWriter writer = resp.getWriter();
        //获取表单数据
        String username = req.getParameter("username");
        // 查看当前会话是否已有账号登录
        MyBindingListener loginedDemo = (MyBindingListener) req.getSession().getAttribute("onlineUserBindingListener");
        //当前会话已有账号登录
        if ("".equals(username) || username == null) {
            System.out.println("非法操作，您没有输入用户名");
            resp.sendRedirect("/sessionBindingDemo/login.html");
        } else {
            if (loginedDemo != null) {
                String logined = loginedDemo.getUsername();
                System.out.println("您已经登录，重复登录无效，请先退出当前账号重新登录！");
                writer.write("<h1>编程帮 www.biancheng.net</h1>"
                        + "<h3>您好，您已经登录了账户：" + logined + "</h3>"
                        + "如要登录其他账号，请先退出当前账号重新登录！");
                // 登陆页面为填写内容
            } else {
                //将当前账号加入会话中
                req.getSession().setAttribute("onlineUserBindingListener", new MyBindingListener(username));
                writer.write("<h1>编程帮 www.biancheng.net</h1>"
                        + "<h3>" + username + "：   欢迎您的到来</h3>");
            }
            //向页面输出结果
            //从上下文中获取已经登录账号的集合
            List<String> onLineUserList = (List<String>) getServletContext().getAttribute("onlineUserList");
            System.out.println("当前在线用户为：");
            if (onLineUserList != null) {
                for (int i = 0; i < onLineUserList.size(); i++) {
                    System.out.println(onLineUserList.get(i));
                }
            }
            System.out.println("********************************");
            writer.write("<h1>   当前在线人数为：" + onLineUserList.size() + "</h1>"
                    + "<table border=\"1\" width=\"50%\">");
            for (int i = 0; i < onLineUserList.size(); i++) {
                writer.write("<tr>\r\n" +
                        "<td align=\"center\">" + onLineUserList.get(i) + " </td>\r\n" +
                        "</tr>");
            }
            writer.write("</table><br/>"
                    + "<a href=\"/sessionBindingDemo/LogoutServlet\">退出登录</a>");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
